package com.ruoyi.web.controller.system;

import java.util.*;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.UserData;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IUserDataService;
import com.ruoyi.system.service.IdouYinUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WeixinData;
import com.ruoyi.system.service.IWeixinDataService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 数据信息Controller
 * 
 * @author ruoyi
 * @date 2020-03-29
 */
@Controller
@RequestMapping("/system/data")
public class WeixinDataController extends BaseController
{
    private String prefix = "system/data";

    @Autowired
    private IWeixinDataService weixinDataService;
    @Autowired
    private IUserDataService iUserDataService;
    @Autowired
    private ISysUserService iSysUserService;


    @RequiresPermissions("system:data:view")
    @GetMapping()
    public String data()
    {
        return prefix + "/data";
    }

    /**
     * 查询数据信息列表
     */
    @RequiresPermissions("system:data:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WeixinData weixinData)
    {
        String username = ShiroUtils.getLoginName();
        startPage();
        List<WeixinData> list = weixinDataService.selectWeixinDataList(weixinData,username);
        return getDataTable(list);
    }

    /**
     * 导出数据信息列表
     */
    @RequiresPermissions("system:data:export")
    @Log(title = "数据信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WeixinData weixinData)
    {
        String username = ShiroUtils.getLoginName();
        List<WeixinData> list = weixinDataService.selectWeixinDataList(weixinData,username);
        ExcelUtil<WeixinData> util = new ExcelUtil<WeixinData>(WeixinData.class);
        return util.exportExcel(list, "data");
    }

    /**
     * 转移数据信息
     */
    @RequestMapping("/transto/{id}")
    public String transto(@PathVariable Long id, Model model)
    {
        UserData userData = iUserDataService.selectUserDataBybid(id);

        Long aLong = userData.getuId();

        SysUser sysUser1 = iSysUserService.selectUserById(aLong);
        String username= sysUser1.getLoginName();

        List<SysUser> list= weixinDataService.findNameAll();

        HashMap<Long, String> map = new HashMap<>();

        for (SysUser sysUser : list) {
            if (!sysUser.getLoginName().equals("admin")&!sysUser.getLoginName().equals(username)&sysUser.getDelFlag().equals("0")){
                map.put(sysUser.getUserId(),sysUser.getLoginName());
            }
        }
        model.addAttribute("user",map);
        model.addAttribute("msg",username);
        model.addAttribute("id",id);
        return prefix + "/transTo";
    }

    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /**
     * 新增保存数据信息
     */
    @RequiresPermissions("system:data:add")
    @Log(title = "数据信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WeixinData weixinData)
    {
        String username = ShiroUtils.getLoginName();
        SysUser user = iSysUserService.selectUserByLoginName(username);
        Long userId = user.getUserId();
        weixinDataService.insertWeixinData(weixinData);
        Long id1 = weixinData.getId();
        iUserDataService.insertUserDataById(id1,userId);
        return toAjax(true);
    }

    /**
     * 修改数据信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WeixinData weixinData = weixinDataService.selectWeixinDataById(id);
        mmap.put("weixinData", weixinData);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据信息
     */
    @RequiresPermissions("system:data:edit")
    @Log(title = "数据信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WeixinData weixinData)
    {
        return toAjax(weixinDataService.updateWeixinData(weixinData));
    }

    /**
     * 删除数据信息
     */
    @RequiresPermissions("system:data:remove")
    @Log(title = "数据信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        iUserDataService.deleteUserDataByIds(ids);
        return toAjax(weixinDataService.deleteWeixinDataByIds(ids));
    }
    /**
     * 转移数据
     */
    @RequiresPermissions("system:data:transto")
    @Log(title = "数据信息", businessType = BusinessType.transto)
    @PostMapping( "/transto")
    @ResponseBody
    public AjaxResult transto(int id,int uId)
    {
        int bId = uId;
        int uid = id;
        return toAjax(weixinDataService.updateUserdata(bId,uid));

    }
    @GetMapping("/userdata/{id}")
    public String userdata(@PathVariable Long id, Model model)
    {
        UserData userData = iUserDataService.selectUserDataBybid(id);
        Long aLong = userData.getuId();
        SysUser sysUser = iSysUserService.selectUserById(aLong);
        model.addAttribute("userdata",sysUser);
        model.addAttribute("id",id);
        return prefix + "/userData";

    }
    /**
     * 查询用户信息
     */
    //@RequiresPermissions("system:data:transto")
    @Log(title = "数据信息", businessType = BusinessType.transto)
    @PostMapping( "/userdata/{id}")
    @ResponseBody
    public TableDataInfo userdatas(@PathVariable Long id,Model model)
    {
        UserData userData = iUserDataService.selectUserDataBybid(id);
        Long aLong = userData.getuId();
        SysUser sysUser = iSysUserService.selectUserById(aLong);
        model.addAttribute("id",id);
        ArrayList<SysUser> list = new ArrayList<>();
        list.add(sysUser);
    return getDataTable(list);
    }
}
